 /**日志存储
  * @author lls
  */

const log4js = require('log4js')
const levels = {
  'trace': log4js.levels.TRACE,
  'debug': log4js.levels.DEBUG,
  'info': log4js.levels.INFO,
  'warn': log4js.levels.WARN,
  'error': log4js.levels.ERROR,
  'fatal': log4js.levels.FATAL
}

// 追加器,对log进行配置 
log4js.configure({
  // appenders: { cheese: { type: "file", filename: "cheese.log" } },
  // categories: { default: { appenders: ["cheese"], level: "error" } }
  appenders: {
    console: {
      type: 'console'
    },
    info: {
      type: 'file',
      filename: 'logs/all-logs.log',
    },
    error: {
      type: 'dateFile',
      filename: 'logs/log',
      pattern: 'yyyy-MM-dd.log', // 按日期滚动
      alwaysIncludePattern: true
    }
  },
  categories: {
    default: { appenders: ['console'], level: 'debug'},
    info: {
      appenders: ['info', 'console'],
      level: 'info'
    },
    error: {
      appenders: ['error', 'console'],
      level: 'error'
    }
  }
})

/**
 * 封装输出函数,日志输出
 * level = debug
 * @param {string} content 
 */
exports.debug = (content) => {
  let logger = log4js.getLogger()
  logger.level = levels.debug
  logger.debug(content)
}

/**
 * 封装输出函数,日志输出
 * level = error 
 * @param {string} content 
 */
 exports.error = (content) => {
  let logger = log4js.getLogger('error')
  logger.level = levels.error
  logger.error(content)
}

/**
 * 封装输出函数,日志输出
 * level = info  
 * @param {string} content 
 */
 exports.info = (content) => {
  let logger = log4js.getLogger('info')
  logger.level = levels.info
  logger.info(content)
}